/*****************************************************************************
 *
 * Copyright (c), 2004-2020,ShangHai Neotune Information Technology Co., Ltd.
 *
 * Module Name : mib.h
 *
 * Author :    sean
 *
 * Date :    2004-02-05
 *
 * Version :    1.00
 *
 * Description :    
 *
 * Other :    
 *
 * Data Structure List: <Main Data Structure>
 *
 * Modification History
 * ---------------------------------------------------------------------------
 *  1: Data :      2004-02-05
 *    Author :     sean
 *    Version :    V100R001M00
 *    Modification : creation
 *
*****************************************************************************/


#if (!defined(mib_inc))
#define mib_inc

#ifdef __cplusplus
extern"C" {
#endif

#if (!defined(asn1_inc))
#include <asn1.h>
#endif

/* Definitions for "form" parameter to the "testproc" found in leaf nodes */
#define TEST_GET        0
#define TEST_SET        1
#define TEST_CREATE     2

/* Definitions for node_type in MIBNODE_T and MIBLEAF_T */
#define NODE_TYPE       0x01
#define INNER_NODE      0x00
#define LEAF_NODE       0x01
#define REMOVABLE       0x02
#define REPLACEABLE     0x04
#define SYSTEM_OWNED    0x08
#define NODE_DYNAMIC    0x10
#define LEAF_DYNAMIC    0x10
#define ARC_DYNAMIC     0x20
#define COOKIE_DYNAMIC  0x20
#define LEAF_INACTIVE   0x40
#define AGENTX_LEAF     0x80

struct SNMP_PKT_S;
struct VB_S;
typedef void ASY_TESTPROC_T __((OIDC_T lastmatch, int tcount, OIDC_T *tlist,
                                struct SNMP_PKT_S *pktp, struct VB_S *vbp));
typedef void ASY_GETPROC_T  __((OIDC_T lastmatch, int tcount, OIDC_T *tlist,
                                struct SNMP_PKT_S *pktp, struct VB_S *vbp));
typedef void ASY_SETPROC_T  __((OIDC_T lastmatch, int tcount, OIDC_T *tlist,
                                struct SNMP_PKT_S *pktp, struct VB_S *vbp));
typedef void ASY_NEXTPROC_T __((OIDC_T lastmatch, int tcount, OIDC_T *tlist,
                                struct SNMP_PKT_S *pktp, struct VB_S *vbp));

typedef int      TESTPROC_T __((int form, OIDC_T lastmatch, int tcount,
                                OIDC_T *tlist, char *cookie, 
                                struct SNMP_PKT_S *pktp, int indx));
typedef INT_32_T GETPROC_T  __((OIDC_T lastmatch, int tcount, OIDC_T *tlist,
                                char *cookie,
                                struct SNMP_PKT_S *pktp));
typedef void     SETPROC_T  __((OIDC_T lastmatch, int tcount, OIDC_T *tlist,
                                char *cookie, INT_32_T value,
                                struct SNMP_PKT_S *pktp, int vb_index));
typedef int      NEXTPROC_T __((OIDC_T lastmatch, int tcount, OIDC_T *tlist,
                                OIDC_T *rlist, char *cookie,
                                struct SNMP_PKT_S *pktp));

#ifdef NEOSNET_STRUCT_ALIGN
#pragma align 1
#endif

/* Define a MIB leaf node */
typedef struct  MIBLEAF_S
        {
        unsigned short  node_type;      /* See above                    */
        OCTET_T         expected_tag;   /* VT_xxx from snmp.h           */
        OCTET_T         access_type;    /* See below                    */

        ASY_TESTPROC_T *testproc;       /* Routine to check whether the */
                                        /* indicated data object exists */
        ASY_GETPROC_T  *getproc;        /* Routine to get the data      */
        ASY_SETPROC_T  *setproc;        /* Routine to set the data      */
        ASY_NEXTPROC_T *nextproc;       /* Routine to locate "next" obj id  */
        char *          user_cookie;    /* Value to pass to unchanged       */
        UINT_16_T       locator;        /* The user can define any value for*/
                                        /*  this field.  It is useful to    */
                                        /*  give easy to handle names to    */
                                        /*  leaves.                         */
        UINT_32_T       view_mask;      /* A mask of the views of which this*/
                                        /* leaf is a part.                  */
        UINT_32_T       write_mask;     /* That subset of view_mask in which*/
                                        /* is potentially writable.         */
#if INSTALL_ENVOY_AGENTX_MASTER
        bits32_t        session_id;     /* session for this object */
        bits16_t        ref_count;      /* reference count              */
        bits8_t         timeout;        /* timeout period               */
#endif

#if defined(DEBUG)
        char *          object_name;    /* FOR DEBUGGING                    */
#endif
        } MIBLEAF_T;

/* Bit values for access_type */
#define READ_ACCESS     0x01
#define WRITE_ACCESS    0x02
#define CREATE_ACCESS   0x04
#define NOTIFY_ACCESS   0x08
#define READ_WRITE_ACCESS (READ_ACCESS | WRITE_ACCESS)
#define READ_CREATE_ACCESS (READ_ACCESS | WRITE_ACCESS | CREATE_ACCESS)

#if INSTALL_ENVOY_TWO_CHAR_MACROS
#define RO      READ_ACCESS                     /* READ ONLY ACCESS     */
#define WO      WRITE_ACCESS                    /* WRITE ONLY ACCESS    */
#define RW      (READ_ACCESS | WRITE_ACCESS)    /* READ/WRITE ACCESS    */
#define RC      (READ_ACCESS | WRITE_ACCESS | CREATE_ACCESS) /* READ/CREATE */
#define NO      NOTIFY_ACCESS                   /* NOTIFY ONLY ACCESS   */
#endif

/* Bit values for scalar vs tabular type */
#define SCALAR  0x80
#define TABULAR 0x00

/* maximum size for a reference count */
#define AX_MAX_REF_COUNT 0xFFFF

/* Define an MIB internal (or root) node */
typedef struct  MIBNODE_S
        {
        unsigned short  node_type;      /* See above                        */
        struct MIBARC_S *arcs;          /* Descendents from this node       */
#if INSTALL_ENVOY_AGENTX_MASTER
        MIBLEAF_T       *leaf;          /* leaf for this node */
#endif
        } MIBNODE_T;

/* Define a pointer from a node to subsidiary node or a leaf    */
/* These structures are aggregated into an array.               */
/* THE LAST ELEMENT IN SUCH AN ARRAY MUST HAVE A NULL POINTER.  */
typedef struct  MIBARC_S
        {
        OIDC_T          id;             /* Object identifier component  */
        struct MIBARC_S *nodep;         /* The descendent node/leaf     */
        } MIBARC_T;

/* Define a cookie structure to hold old style method routines for use with
   the backwards compatibility method routines */
typedef struct COMPAT_ROUTINES_S 
        {
        TESTPROC_T *    testproc;
        NEXTPROC_T *    nextproc;
        GETPROC_T  *    getproc;
        SETPROC_T  *    setproc;
        char       *    cookie;
        } COMPAT_ROUTINES_T;

#define MIB_ISO         1       /* Level 0 - ISO *IS* #1 by fiat (!!)   */
#define MIB_CCITT       2       /* Level 0                              */
#define MIB_JOINT_ISO_CCITT     3       /* Level 0                      */
#define MIB_ORG         3       /* Level 1 - {ISO 3}                    */
#define MIB_DOD         6       /* Level 2 - {ORG 6}                    */
#define MIB_INTERNET    1       /* Level 3 - {DOD 1}                    */


#define MIB_DIRECTORY   1       /* Level 4 - {INTERNET 1}               */
#define MIB_MGMT        2       /* Level 4 - {INTERNET 2}               */
#define MIB_MIB2        1       /* Level 5 - {MGMT 1}                   */
#define MIB_MIB1        1       /* Level 5 - {MGMT 1}                   */
#define MIB_SYSTEM      1       /* Level 6 - {MIB 1}                    */
#define MIB_INTERFACES  2       /* Level 6 - {MIB 2}                    */
#define MIB_AT          3       /* Level 6 - {MIB 3}                    */
#define MIB_IP          4       /* Level 6 - {MIB 4}                    */
#define MIB_ICMP        5       /* Level 6 - {MIB 5}                    */
#define MIB_TCP         6       /* Level 6 - {MIB 6}                    */
#define MIB_UDP         7       /* Level 6 - {MIB 7}                    */
#define MIB_EGP         8       /* Level 6 - {MIB 8}                    */

#define MIB_EXPERIMENTAL        3       /* Level 4 - {INTERNET 3}       */
#define MIB_IETF        1       /* Level 5 - {EXPERIMENTAL 1}           */ 
#define MIB_PRIVATE     4       /* Level 4 - {INTERNET 4}               */
#define MIB_ENTERPRISE  1       /* Level 5 - {PRIVATE 1}                */


#define ARC(ID,NODE)               {ID, (struct MIBARC_S *)&NODE}
#define END_OF_ARC_LIST            {0, (struct MIBARC_S *)0}
#define NODE(NAME, ARCLIST)        {INNER_NODE, ARCLIST}
#define NODE_NT(NAME, NT, ARCLIST) {(INNER_NODE | NT), ARCLIST}
#define NULLPROC                   (int (*)())0

/* The LEAF macro is for release 1 mib.c and MIB Compiler */
#define LEAF(NAME, VT, ACCESS, TESTP, GETP, SETP, NXTP, COOKIE) \
                                {LEAF_NODE, VT, ACCESS,         \
                                (TESTPROC_T *)TESTP, (GETPROC_T *)GETP, \
                                (SETPROC_T *)SETP, (NEXTPROC_T *)NXTP,  \
                                (char *)COOKIE, 0, 0xFFFFFFFFL, 0xFFFFFFFFL}

/* The LEAFM macro is for release 2 mib.c and MIB Compiler */
#define LEAFM(NM, VT, ACC, TSTP, GETP, SETP, NXTP, CKE, LOC, VMSK, WMSK)  \
                                {LEAF_NODE, VT, ACC,                    \
                                (TESTPROC_T *)TSTP, (GETPROC_T *)GETP, \
                                (SETPROC_T *)SETP, (NEXTPROC_T *)NXTP,  \
                                (char *)CKE, LOC, VMSK, WMSK }

/* The LEAF_ASYS macro is for release envoy release 6 and is for use with
   the async routines, it tags the leaf as static */
#define LEAF_ASYS(LN, NM, VT, AC, TB, TST, GET, SET, NEX, CKE, LOC, VMK, WMK) \
STATIC MIBLEAF_T         LN = {LEAF_NODE, VT, AC,                           \
                               (ASY_TESTPROC_T *)TST, (ASY_GETPROC_T *)GET, \
                               (ASY_SETPROC_T *)SET, (ASY_NEXTPROC_T *)NEX, \
                               (char *)CKE, LOC, VMK, WMK }

/* The LEAF_ASY macro is for release envoy release 6 and is for use with
   the async routines */
#define LEAF_ASY(LN, NM, VT, AC, TB, TST, GET, SET, NEX, CKE, LOC, VMK, WMK) \
MIBLEAF_T        LN = {LEAF_NODE, VT, AC,                           \
                       (ASY_TESTPROC_T *)TST, (ASY_GETPROC_T *)GET, \
                       (ASY_SETPROC_T *)SET, (ASY_NEXTPROC_T *)NEX, \
                       (char *)CKE, LOC, VMK, WMK }

/* The LEAFCOMS macro is for envoy release 6 and is for use with the async
   compatibility routines.  The compat routines allow older non-async method
   routines to be run from a newer async engine */
#define LEAF_COMS(LN, CN, NM, VT, AC, TB, TS, GT, ST, NX, CKE, LOC, VMK, WMK) \
STATIC COMPAT_ROUTINES_T CN = {(TESTPROC_T *)TS, (NEXTPROC_T *)NX,      \
                               (GETPROC_T *)GT,  (SETPROC_T *)ST,       \
                               (char *)CKE };                           \
STATIC MIBLEAF_T         LN = {LEAF_NODE, VT, AC,                       \
                               (ASY_TESTPROC_T *)async_compat_test,     \
                               (ASY_GETPROC_T *)async_compat_get,       \
                               (ASY_SETPROC_T *)async_compat_set,       \
                               (ASY_NEXTPROC_T *)async_compat_next,     \
                               (char *)&CN, LOC, VMK, WMK }

#define LEAF_COM(LN, CN, NM, VT, AC, TB, TS, GT, ST, NX, CKE, LOC, VMK, WMK) \
STATIC COMPAT_ROUTINES_T CN = {(TESTPROC_T *)TS, (NEXTPROC_T *)NX,      \
                               (GETPROC_T *)GT,  (SETPROC_T *)ST,       \
                               (char *)CKE };                           \
MIBLEAF_T                LN = {LEAF_NODE, VT, AC,                       \
                               (ASY_TESTPROC_T *)async_compat_test,     \
                               (ASY_GETPROC_T *)async_compat_get,       \
                               (ASY_SETPROC_T *)async_compat_set,       \
                               (ASY_NEXTPROC_T *)async_compat_next,     \
                               (char *)&CN, LOC, VMK, WMK }

/* The LEAF NT macros are for emissary release 7 and include a flag
   argument to allow setting of the removable class flags */
#define LEAF_NT_ASYS(LN, NT, NM, VT, AC, TB, TS, GT, ST, NX, CK, LC, VM, WM) \
STATIC MIBLEAF_T         LN = {(LEAF_NODE | NT), VT, AC,                    \
                               (ASY_TESTPROC_T *)TS, (ASY_GETPROC_T *)GT, \
                               (ASY_SETPROC_T *)ST, (ASY_NEXTPROC_T *)NX, \
                               (char *)CK, LC, VM, WM }

#define LEAF_NT_ASY(LN, NT, NM, VT, AC, TB, TS, GT, ST, NX, CK, LC, VM, WM) \
MIBLEAF_T        LN = {(LEAF_NODE | NT), VT, AC,                            \
                       (ASY_TESTPROC_T *)TS, (ASY_GETPROC_T *)GT, \
                       (ASY_SETPROC_T *)ST, (ASY_NEXTPROC_T *)NX, \
                       (char *)CK, LC, VM, WM }

/* The COM macros are for use with the async compatibility routines.
   The compat routines allow older non-async method routines to
   be run from a newer async engine */
#define LEAF_NT_COMS(LN, CN, NT, NM, VT, AC, TB, TS, GT, ST, NX, CK, LC, VM, WM) \
STATIC COMPAT_ROUTINES_T CN = {(TESTPROC_T *)TS, (NEXTPROC_T *)NX,      \
                               (GETPROC_T *)GT,  (SETPROC_T *)ST,       \
                               (char *)CK };                            \
STATIC MIBLEAF_T         LN = {(LEAF_NODE | NT), VT, AC,                \
                               (ASY_TESTPROC_T *)async_compat_test,     \
                               (ASY_GETPROC_T *)async_compat_get,       \
                               (ASY_SETPROC_T *)async_compat_set,       \
                               (ASY_NEXTPROC_T *)async_compat_next,     \
                               (char *)&CN, LC, VM, WM }

#define LEAF_NT_COM(LN, CN, NT, NM, VT, AC, TB, TS, GT, ST, NX, CK, LC, VM, WM) \
STATIC COMPAT_ROUTINES_T CN = {(TESTPROC_T *)TS, (NEXTPROC_T *)NX,      \
                               (GETPROC_T *)GT,  (SETPROC_T *)ST,       \
                               (char *)CK };                            \
MIBLEAF_T                LN = {(LEAF_NODE | NT), VT, AC,                \
                               (ASY_TESTPROC_T *)async_compat_test,     \
                               (ASY_GETPROC_T *)async_compat_get,       \
                               (ASY_SETPROC_T *)async_compat_set,       \
                               (ASY_NEXTPROC_T *)async_compat_next,     \
                               (char *)&CN, LC, VM, WM }



typedef struct  MIBLOC_S
        {
        UINT_16_T               ml_flags;
        OIDC_T                  ml_last_match;
        /* The following item splits out the "instance" part of the     */
        /* full object identifier.  The number of components in the     */
        /* "base" part (i.e. the path to the leaf may be derived by     */
        /* vb_obj_id.num_components - ml_base_objid.num_components and  */
        /* the list of components being taken directly from             */
        /* vb_obj_id.component_list.                                    */
        OBJ_ID_T                ml_remaining_objid;
        union {
              MIBLEAF_T         *mlleaf_u;
              MIBNODE_T         *mlnode_u;
              } mbl_u;
        } MIBLOC_T;
#define ml_leaf         mbl_u.mlleaf_u
#define ml_node         mbl_u.mlnode_u

#ifdef NEOSNET_STRUCT_ALIGN
#pragma align 0
#endif

/* Values for ml_flags */
#define ML_IS_LEAF      0x01

/* Determine what the default root node will be */
#ifdef ENVOY_DEFAULT_MIB_ROOT_NODE
#define ENVOY_MIB_ROOT_NODE ENVOY_DEFAULT_MIB_ROOT_NODE
extern  MIBNODE_T           ENVOY_DEFAULT_MIB_ROOT_NODE;
#else
#define ENVOY_MIB_ROOT_NODE mib_root_node
extern  MIBNODE_T           mib_root_node;
#endif

extern  int        SNMP_FindNodeFromRoot   __((MIBNODE_T *root,
                                                  OBJ_ID_T  *objp,
                                                  MIBNODE_T **npp));

extern  int             SNMP_AddNodeFromRoot    __((MIBNODE_T *root,
                                                  OBJ_ID_T  *objp,
                                                  MIBNODE_T *anode,
                                                  MIBNODE_T **rnode));

extern  MIBNODE_T *     SNMP_RemoveNodeFromRoot __((MIBNODE_T *root,
                                                  OBJ_ID_T  *objp));

#if (INSTALL_ENVOY_AGENTX_MASTER)
extern sbits32_t        Find_Leaf_From_Root   __((MIBNODE_T *mibroot,
                                                  OBJ_ID_T  *objid,
                                                  MIBLEAF_T **mibleaf));

extern int              AX_Add_Leaf_From_Root __((MIBNODE_T *mibroot,
                                                  OBJ_ID_T  *objp,
                                                  MIBLEAF_T *add_leaf,
                                                  MIBLEAF_T **rem_leaf));

extern MIBLEAF_T *      AX_Remove_Leaf_From_Root __((MIBNODE_T *mibroot,
                                                     OBJ_ID_T  *objid));

#endif /* #if (INSTALL_ENVOY_AGENTX_MASTER) */

#define Find_Node(O, N)   SNMP_FindNodeFromRoot   (0, O, N)

#define Add_Node(O, A, R) SNMP_AddNodeFromRoot    (0, O, A, R)

#define Remove_Node(O)    SNMP_RemoveNodeFromRoot (0, O)

#ifdef __cplusplus
}
#endif

#endif  /* mib_inc */


